<html>
<head>
    <title>Deferred Spotlight Shadow</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="../dist/claygl.js"></script>
</head>
<body style="margin:0px; background-color: black">
    <canvas id="main"></canvas>

    <script type="text/javascript">
        var renderer = new clay.Renderer({
            canvas: document.getElementById('main')
        });
        renderer.resize(window.innerWidth, window.innerHeight);
        var shadowMapPass = new clay.prePass.ShadowMap();
        var deferredRenderer = new clay.deferred.Renderer({
            shadowMapPass: shadowMapPass
        });

        var timeline =  new clay.Timeline();
        timeline.start();

        var GLTFLoader = new clay.loader.GLTF({
            includeLight: false
        });

        GLTFLoader.load("assets/models/basic_scene/scene.gltf");

        GLTFLoader.on('success', function(res) {
            var scene = res.scene;
            scene.rotation.rotateX(-Math.PI / 2);
            var camera = new clay.camera.Perspective({
                aspect: renderer.getViewportAspect(),
                far: 500
            });

            camera.position.set(2, 2, 2);
            camera.lookAt(scene.position);
            camera.aspect = renderer.canvas.width / renderer.canvas.height;

            var control = new clay.plugin.OrbitControl({
                target: camera,
                domElement: renderer.canvas,
                sensitivity: 0.4
            });

            var light = new clay.light.Spot({
                color: [1, 0.3, 0.3],
                range: 30,
                umbraAngle: 10,
                penumbraAngle: 25,
                intensity: 1,
                shadowResolution: 512,
                shadowBias: 0.001,
                castShadow: true
            });
            light.position.set(-3, -4, 2);
            light.lookAt(scene.position);
            scene.add(light);

            var light2 = new clay.light.Spot({
                color: [0.3, 1, 0.3],
                range: 50,
                umbraAngle: 10,
                penumbraAngle: 25,
                shadowResolution: 512,
                intensity: 1,
                shadowBias: 0.001,
                castShadow: true
            });
            light2.position.set(3, -4, 2);
            light2.lookAt(scene.position);
            scene.add(light2);

            var light3 = new clay.light.Point({
                color: [0.3, 0.3, 1],
                range: 50,
                shadowResolution: 512,
                shadowBias: 0.001,
                castShadow: true
            });
            light3.position.set(0, -4, 5);
            light3.lookAt(scene.position);
            scene.add(light3);


            timeline.on('frame', function(deltaTime) {
                control.update(deltaTime);
                // shadowMapPass.render(renderer, scene, camera);
                // renderer.render(scene, camera);
                deferredRenderer.render(renderer, scene, camera);
            });
        });
    </script>
</body>
</html>